1070K - Video Posts - CodeForces Solution


implementation *1100

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())

durations = list(map(int, input().split()))

dsum = sum(durations)
segmentsum = dsum / k

runningtotal = 0
segmentsize = 0
numsegments = 0
output = "Yes"
sizes = []
for i, duration in enumerate(durations):
  runningtotal += duration
  segmentsize += 1
  if runningtotal > segmentsum:
    output = "No"
    break
  elif runningtotal == segmentsum:
    runningtotal = 0
    numsegments += 1
    sizes.append(segmentsize)
    segmentsize = 0

if output == "Yes" and runningtotal == 0 and numsegments == k:
  print("Yes")
  for size in sizes[:-1]:
    print(size, end=" ")
  print(sizes[-1])
else:
  print("No")
 	 	 	 			 	  		    	 			  	 	

C++ Code:

#include<bits/stdc++.h>
using namespace std;
//
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ll long long 
#define mod 1000000007
#define mp make_pair
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define cinv(a) for(int i=0;i<a.size();i++){ cin>>a[i];}
#define coutv(a) for(int i=0;i<a.size();i++){cout<<a[i]<<" ";}

void solve() {
int n,k;
cin>>n>>k;
vector<int> a(n);
cinv(a);
int sum=0;
for(int i=0;i<n;i++){
    sum+=a[i];
}

if(sum%k==0){
   
    
bool f=true;
int index=0;
int tempsum=0;
sum=sum/k;
vector<int> ans;
int counts=0;
while(index<n){
tempsum+=a[index];
counts++;
if(tempsum==sum){

    tempsum=0;
    ans.push_back(counts);
    counts=0;
}
else if(tempsum>sum){
    f=false;
    break;
}
index++;

}
if(f){
    cout<<"Yes"<<endl;
    for(int i=0;i<k;i++){
        cout<<ans[i]<<" ";
    }
    cout<<"\n";
}
else{
    cout<<"No"<<endl;

}


}
else{
    cout<<"No"<<endl;
    return ;
}


}

int main(){

flash 
int tc;
tc=1;
while(tc--){
solve();
}
    return 0;
}


Comments

Submit
0 Comments
More Questions

1167C - News Distribution
813C - The Tag Game
1130C - Connect
1236B - Alice and the List of Presents
845C - Two TVs
1144D - Equalize Them All
298A - Snow Footprints
1753B - Factorial Divisibility
804A - Find Amir
1541C - Great Graphs
607B - Zuma
30A - Accounting
959C - Mahmoud and Ehab and the wrong algorithm
1215A - Yellow Cards
237B - Young Table
1216D - Swords
271D - Good Substrings
573A - Bear and Poker
10A - Power Consumption Calculation
1244B - Rooms and Staircases
777A - Shell Game
1698D - Fixed Point Guessing
415B - Mashmokh and Tokens
26D - Tickets
471B - MUH and Important Things
982B - Bus of Characters
1102B - Array K-Coloring
818A - Diplomas and Certificates
70A - Cookies
798A - Mike and palindrome